package com.cos.web.controller;

import com.sun.deploy.util.SessionState;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author: COS
 * @time: 2021/5/19 14:31
 * @description:
 */
@Controller
@RequestMapping("/demo2")
//@SessionAttributes({"name"})
public class Demo3Controller {

    public String d1(String name, SessionStatus state){
            name="xxx";
//        state.isComplete(); 情况session作用域的值
            return "";
    }


    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 14:35
     * @description: 传递参数到前端 1
     * HttpServletRequest
     */
    @RequestMapping("/d1")
    public String d1(HttpServletRequest request){
        String name="张三";
        request.setAttribute("name", name);
        return "index";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 14:35
     * @description: 传递参数到前端 2
     *  ModelAndView
     */
    @RequestMapping("/d2")
    public ModelAndView d2(){
        //1创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        //2 设置逻辑视图名
        mv.setViewName("index");
        //3 设置要传递的参数
        String name = "李四";
        mv.addObject("name", name);
        mv.addObject(name);
        //4 返回ModelAndView对象
        return mv;
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 14:42
     * @description: 传递参数到前端 3
     *  Model 接口对象,自动帮我们把数据放到request作用域中
     */
    @RequestMapping("/d3")
    public String d3(Model model){
        model.addAttribute("name", "王五");
        return "index";
    }

    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 14:44
     * @description: 传递参数到前端 4
     *      *  MAP集合对象,自动帮我们把数据放到request作用域中
     *      如果加上注解@RequestParam map编程接受参数用的
     */
    @RequestMapping("/d4")
    public String d4( Map<String,Object> maps){
        maps.put("name", "赵六");
        return "index";
    }
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/5/19 14:47
     * @description:
     */
    @RequestMapping("/d5")
    public String d5(HttpSession session){
        session.setAttribute("name", "田七");
        return "demo";
    }
}
